<?php
namespace Modules\Base\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Services\WechatService;
use Illuminate\Http\Request;
use Modules\Base\Entities\BaseReply;
use Modules\Base\Http\Requests\BaseReplyRequest;
use Modules\Weixin\Entities\WxKeywords;
use Modules\Weixin\Entities\WxRule;

class BaseReplyController extends Controller
{
    //显示列表
    public function index()
    {
        $data = BaseReply::paginate(10);
        return view('base::base_reply.index', compact('data'));
    }

    //创建视图
    public function create(BaseReply $base_reply,WechatService $wechatService)
    {
    	$views=$wechatService->ruleView ();
        return view('base::base_reply.create',compact('base_reply','views'));
    }

    //保存数据
    public function store(BaseReplyRequest $request,BaseReply $base_reply,WechatService $wechatService)
    {
		\DB::beginTransaction();
		$rule_id=$wechatService->saveRule ();
		BaseReply::create([
			'content'=>$request->input('content'),
			'rule_id'=>$rule_id
		]);
		\DB::commit ();
        return redirect('/base/base_reply')->with('success', '保存成功');
    }

    //显示记录
    public function show(BaseReply $field)
    {
        return view('base::base_reply.show', compact('field'));
    }

    //编辑视图
    public function edit(BaseReply $base_reply,WechatService $wechatService)
    {
		$views=$wechatService->ruleView ($base_reply['rule_id']);
        return view('base::base_reply.edit', compact('base_reply','views'));
    }

    //更新数据
    public function update(BaseReplyRequest $request, BaseReply $base_reply,WechatService $wechatService)
    {
		\DB::beginTransaction();
    	$rule_id=$wechatService->saveRule ($base_reply['rule_id']);
    	$base_reply->content=$request->input('content');
    	$base_reply->rule_id=$rule_id;
    	$base_reply->save();
		\DB::commit ();
        return redirect('/base/base_reply')->with('success','更新成功');
    }

    //删除模型
    public function destroy(BaseReply $base_reply)
    {
        $base_reply->delete();
        WxRule::destroy ($base_reply['rule_id']);
        return redirect('base/base_reply')->with('success','删除成功');
    }
}
